package cn.fl.continfo.service.impl;

import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.fl.commons.result.ResponseData;
import cn.fl.continfo.service.IPrjContCfgDtlService;
import cn.fl.continfo.vo.PrjContCfgDtlVO;
import cn.fl.continfo.vo.PrjContCfgVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.preloan.IPrjContCfgApiService;
import cn.fl.preloan.IPrjContCfgDtlApiService;
import cn.fl.preloan.condition.PrjContCfgDtlQueryCondition;
import cn.fl.preloan.condition.PrjContCfgQueryCondition;
import cn.fl.preloan.dto.PageResponseDTO;
import cn.fl.preloan.dto.PrjContCfgDTO;
import cn.fl.preloan.dto.PrjContCfgDtlDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class PrjContCfgDtlServiceImpl implements IPrjContCfgDtlService {

    @Autowired
    private IPrjContCfgDtlApiService prjContCfgDtlApiService;

    @Override
    public boolean addBatch(List<PrjContCfgDtlVO> entitys) throws BaseException {
        if(CollectionUtils.isEmpty(entitys)){
            throw new BaseException("新增合同方案明细实体为空");
        }
        List<PrjContCfgDtlDTO> dtoList = new ArrayList<>(entitys.size());
        for(PrjContCfgDtlVO vo : entitys){
            PrjContCfgDtlDTO dto = new PrjContCfgDtlDTO();
            BeanUtils.copyProperties(vo,dto);
            dto.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
            dto.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
            Date date = new Date();
            dto.setCrtTm(date);
            dto.setMdfTm(date);
            dtoList.add(dto);
        }
        ResponseData responseData = prjContCfgDtlApiService.addBatch(dtoList);
        if(Objects.equals(responseData.getCode(),ResponseData.SUCCESS_CODE)){
            return true;
        }
        return false;
    }

    @Override
    public boolean removeBatchById(List<Long> ids) throws BaseException {
        if(CollectionUtils.isEmpty(ids)){
            throw new BaseException("删除合同方案明细id为空");
        }

        ResponseData responseData = prjContCfgDtlApiService.removeBatchById(ids);
        if(Objects.equals(responseData.getCode(),ResponseData.SUCCESS_CODE)){
            return true;
        }
        return false;
    }

    @Override
    public Pager searchListPage(int pageSize, int pageNum, PrjContCfgDtlQueryCondition condition) throws BaseException {
        if(pageSize<1 || pageNum<0){
            throw new BaseException("分页查询合同方案参数异常");
        }
        ResponseData<PageResponseDTO<PrjContCfgDtlDTO>> responseData = prjContCfgDtlApiService.selectListPage(pageSize,pageNum,condition);

        Pager pager = new Pager();
        if(Objects.equals(responseData.getCode(),ResponseData.SUCCESS_CODE)){
            PageResponseDTO<PrjContCfgDtlDTO> pageDto = responseData.getData();
            if(null!=pageDto && CollectionUtils.isNotEmpty(pageDto.getList())){
                List<PrjContCfgDtlVO> list = new ArrayList<>();
                for(Object dto : pageDto.getList()){
                	PrjContCfgDtlVO vo = new PrjContCfgDtlVO();
                    BeanUtils.copyProperties(dto,vo);
                    list.add(vo);
                }
                pager.setTotalCount(pageDto.getTotalCount());
                pager.setResultList(list);
                return pager;
            }
        }
        return pager;
    }

	@Override
	public List<PrjContCfgDtlVO> searchList(PrjContCfgDtlQueryCondition condition) throws BaseException {
		
		ResponseData<List> responseData = prjContCfgDtlApiService.selectList(condition);
		List<PrjContCfgDtlVO> list = new ArrayList<>();
        if(Objects.equals(responseData.getCode(),ResponseData.SUCCESS_CODE)){
            if(CollectionUtils.isNotEmpty(responseData.getData())){
              
                for(Object dto : responseData.getData()){
                	PrjContCfgDtlVO vo = new PrjContCfgDtlVO();
                    BeanUtils.copyProperties(dto,vo);
                    list.add(vo);
                }
            }
        }
        return list;
	}
}
